﻿using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace Assignment5
{
    // 订单实体类
    public class Order
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int OrderId { get; set; }

        public int OrderDetailsId { get; } = GenerateOrderDetailsId();

        public OrderDetails Description { get; set; }

        public int GAmount { get; set; }
        public double GMoney { get; set; }

        public int CustomerId { get; set; }

        [ForeignKey("CustomerId")]
        public Customer OrderCustomer { get; set; }

        // 构造函数
        public Order() : this(null) { }

        public Order(Customer customer)
        {
            Description = new OrderDetails();
            OrderCustomer = customer;
            if (customer != null)
            {
                CustomerId = customer.CustomerId;
            }
        }

        // 添加商品
        public void AddGoods(Goods goods)
        {
            if (goods == null)
            {
                throw new ArgumentNullException(nameof(goods), "商品不能为空");
            }
            Description?.GoodsList.Add(goods);
            if (Description != null)
            {
                Description.Amount++;
                GAmount = Description.Amount;
                GMoney = Description.Money += goods.Price;
            }
        }

        // 重写 ToString 方法
        public override string ToString()
        {
            return $"---ORDER id:{OrderId}\n---Customer information:{OrderCustomer}\n---Order details:\n{Description}";
        }

        // 重写 Equals 方法
        public override bool Equals(object obj)
        {
            if (obj is Order otherOrder)
            {
                return otherOrder.OrderId == OrderId;
            }
            return false;
        }

        // 生成订单详情 ID 
        private static int GenerateOrderDetailsId() => Guid.NewGuid().GetHashCode();
    }

    // 订单详情类
    public class OrderDetails
    {
        [Key]
        public int OrderDetailsId { get; set; }

        public List<Goods> GoodsList { get; } = new List<Goods>();

        public int Amount { get; set; }
        public double Money { get; set; }

        // 重写 ToString 方法
        public override string ToString()
        {
            var sb = new StringBuilder();
            foreach (var good in GoodsList)
            {
                sb.AppendLine(good.ToString());
            }
            return $"{sb}goods count:{Amount}\ntotal money:{Money}";
        }
    }

    // 订单服务类
    public class OrderService
    {
        private readonly MyDbContext _context;

        public OrderService(MyDbContext context)
        {
            _context = context;
        }

        // 添加订单
        public void AddOrder(Order newOrder)
        {
            if (newOrder == null)
            {
                throw new ArgumentNullException(nameof(newOrder), "订单不能为空");
            }
            if (_context.Orders.Any(o => o.OrderId == newOrder.OrderId))
            {
                throw new ArgumentException("订单已存在");
            }
            _context.Orders.Add(newOrder);
            _context.SaveChanges();
        }

        // 删除订单
        public void DeleteOrder(int orderId)
        {
            var order = _context.Orders.Find(orderId);
            if (order == null)
            {
                throw new ArgumentNullException($"订单 ID {orderId} 不存在");
            }
            _context.Orders.Remove(order);
            _context.SaveChanges();
        }

        // 修改订单
        public void ModifyOrder(Order updatedOrder)
        {
            if (updatedOrder == null)
            {
                throw new ArgumentNullException(nameof(updatedOrder), "更新的订单不能为空");
            }
            var order = _context.Orders.Find(updatedOrder.OrderId);
            if (order == null)
            {
                throw new ArgumentNullException($"订单 ID {updatedOrder.OrderId} 不存在");
            }
            order.Description = updatedOrder.Description;
            order.GMoney = updatedOrder.GMoney;
            order.GAmount = updatedOrder.GAmount;
            order.OrderCustomer = updatedOrder.OrderCustomer;
            order.CustomerId = updatedOrder.CustomerId;
            _context.SaveChanges();
        }

        // 查询订单
        public List<Order> QueryOrders(Func<Order, bool> predicate, Func<Order, IComparable> orderBy = null)
        {
            var query = _context.Orders.AsQueryable();
            if (predicate != null)
            {
                query = query.Where(predicate);
            }
            if (orderBy != null)
            {
                query = query.OrderBy(orderBy);
            }
            return query.ToList();
        }

        // 排序
        public List<Order> SortOrders(Func<Order, IComparable> orderBy)
        {
            return _context.Orders.OrderBy(orderBy).ToList();
        }
    }

   
}